<template>
	<div class="system-common-container">
		<el-card shadow="hover">
			<el-form :model="state.tableData" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label='手机号码'>
					<el-input v-model="state.tableData.param.phone" size="default" placeholder="请输入手机号" clearable
						@keyup.enter="handleQuery"> </el-input>
					<i style="margin-right: 20px;"></i>
					<el-checkbox v-model="state.tableData.param.IsAdd">存储</el-checkbox>
				</el-form-item>
				<el-form-item>
					<el-button size="default" type="primary" class="ml10" @click="handleQuery">
						<el-icon>
							<ele-Search />
						</el-icon>
						查询
					</el-button>

				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover" style="margin-top: 8px">
            <div style="margin: -8px 0 10px 0;">
                <el-button size="default" type="warning" plain @click="onOpenAddDialog('add')">
                    <el-icon>
                        <Upload/>
                    </el-icon>
                    上传
                </el-button>
            </div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" stripe>
				<el-table-column prop="phone" label="电话号码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary"
							@click="onOpenEditDialog('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.tableData.param.pageNum" background
				v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total">
			</el-pagination>
		</el-card>
		<BlackListDialog ref="blackListDialogRef" @refresh="handleQuery()" />
	</div>
</template>

<script setup lang="ts" name="blackList">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import apiBlackList from '/@/api/common/blackList/index';

// 引入组件
const BlackListDialog = defineAsyncComponent(() => import('/@/views/common/blackList/component/editBlackList.vue'));

// 定义变量内容
const blackListDialogRef = ref<InstanceType<typeof BlackListDialog>>();
const state = reactive<SysBlackListState>({
	tableData: {
		data: [] as Array<RowBlackListType>,
		total: 0,
		loading: false,
		param: {
			id: '',
			phone: '',
			pageNum: 1,
			pageSize: 20,
			IsAdd: false
		},
	},
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	if (!state.tableData.param.phone) return;

	state.tableData.loading = true;
	let params = Object.assign(state.tableData.param);
	apiBlackList().queryRecord(params).then((res) => {
		if (res.data.resCode != '0000') {
			ElMessage.error(res.data.resMessage);
			return;
		}
		state.tableData.data = res.data.result.items ?? [];
		state.tableData.total = res.data.result?.total;
		state.tableData.loading = false;

	})
		.catch((err: any) => {
		});
};

// 新增弹窗
const onOpenAddDialog = (type: string) => {
	blackListDialogRef.value?.openDialog(type, {
		id: '',
		tenantId: '',
		phone: '',
		createTime: ''
	});
};
// 修改弹窗
const onOpenEditDialog = (type: string, row: RowBlackListType) => {
	blackListDialogRef.value?.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowBlackListType) => {
	ElMessageBox.confirm(`此操作将永久删除：“${row.phone}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			apiBlackList().deletePost(row).then((res) => {
				if (res.data.resCode != '0000')
					ElMessage.error(res.data.resMessage);
				else
					ElMessage.success("删除成功");
				handleQuery();
			});
		})
		.catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	handleQuery();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	handleQuery();
};

</script>

<style scoped lang="scss" >
:deep(.el-checkbox__input) {
	margin-top: -3px;
}
</style>
